package com.dz.scrm.utils;

import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;

import java.util.ArrayList;

public class KingSoftParasJson {
	private static String TAG = "KingSoftParasJson";
	public static <T> Object getObjectByJson(String pData, Class<T> pClass) {
		Object msg = new Object();
		try {
			Gson gson = new Gson();
			msg = gson.fromJson(pData, pClass);
		} catch (Exception e) {
			Log.e(TAG,"===== Paras obj==="+e.toString());
			msg = null;
		}
		return msg;
	}
	public static String[] getArrByJson(String pData) {
		String[] arrTemp =null;
		try {
			JSONArray jsonarray = new JSONArray(pData);
			arrTemp = new String[jsonarray.length()];
			for (int i = 0; i < jsonarray.length(); i++) {
				String data = jsonarray.getString(i);
				arrTemp[i] = data;
			}
		} catch (Exception e) {
			Log.e(TAG,"===== Paras []==="+e.toString());
		}
		return arrTemp;
	}
	
	@SuppressWarnings("unchecked")
	public static <T, E> ArrayList<E> getListByJson(String pData,
                                                    Class<T> pClass) {
		ArrayList<E> msg = new ArrayList<E>();
		try {
//			JSONObject obj = new JSONObject(pData);
//			String info = obj.getString("Date");
//			BaseActivity.Elog(TAG,"pData => "+pData);
			JSONArray jsonarray = new JSONArray(pData);
//			BaseActivity.Elog(TAG,"jsonarray.leng ==> "+jsonarray.length());
			for (int i = 0; i < jsonarray.length(); i++) {
				String data = jsonarray.getString(i);
				Object temp = getObjectByJson(data, pClass);
				msg.add((E) temp);
			}
		} catch (Exception e) {
			Log.e(TAG,"===== Paras List==="+e.toString());
			msg = null;
		}
		return msg;
	}
}
